<?php

namespace App\Http\Controllers\Api;

use App\GroupBuyItem;
use App\Http\Controllers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Merchant;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;

class UserController extends Controller
{
    use ApiResponse;
    public $uid;
    public function __construct()
    {
        $this->uid=auth('api')->id();
    }

    public function group_buy(Request $request)
    {
        $list=GroupBuyItem::with(['group_master'=>function($query){
            $query->with('goods');
        }])->where('uid',$this->uid)->where('status',1)->latest()->paginate($request->get('size',10));
        return $this->data($list);
    }
    public function poster()
    {
        $poster=Merchant::where('uid',$this->uid)->first();
        $app=app('wechat.mini_program');
        if (!file_exists(public_path('images/merchant/merchant_'.$this->uid.'.png'))){
            $response = $app->app_code->getUnlimit("0.9.".$poster['uid'],[
                "page"=>"pages/product/product",
                "is_hyaline"=>true,
                "width"=>300,
                "line_color"=>[
                  'r'=>0,
                  'g'=>0,
                  'b'=>0
                ],
            ]);
            if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
                $filename = $response->saveAs(public_path('images/merchant'), 'merchant_'.$this->uid.'.png');
            }
        }else{
            $filename='merchant_'.$this->uid.'.png';
        }

        $post_img=str_replace(config('app.url'),'',$poster['poster']);
//        dd($post_img);
        $img=Image::make(public_path($post_img))->resize(600,800);
        $img->insert(public_path('images/merchant/merchant_'.$this->uid.'.png'),'top-left',0,0);
        $path=public_path('images/merchant/merchant_'.$poster['uid'].'_qrcode.jpeg');
        $img->save($path);
        $result=[
            'qrcode'=>config('app.url').'/images/merchant/merchant_'.$poster['uid'].'_qrcode.jpeg'
        ];
        return $this->data($result);

    }
}
